nanopyx.methods.channel_registration.estimator

 1import numpy as np
 2from numpy import array
 3from skimage.io import imsave
 4
 5from .corrector import ChannelRegistrationCorrector
 6from ...core.analysis.cross_correlation_elastic import calculate_translation_mask
 7
 8
 9# this class assumes that the image is a numpy array with shape = [n_channels, height, width]
10# assumes that channels in an image that will be aligned using generated translation masks will be in the same order
11class ChannelRegistrationEstimator(object):
12
13    def __init__(self) -> None:
14        self.translation_masks = None
15        self.ccms = None
16
17    def apply_elastic_transform(self, img_stack):
18        corrector = ChannelRegistrationCorrector()
19        return corrector.align_channels(img_stack, translation_masks=self.translation_masks)
20
21    def calculate_translation(self, channel_to_align, ref_channel_img, max_shift, blocks_per_axis, min_similarity, algorithm="field", method="subpixel"):
22        translation_mask = calculate_translation_mask(channel_to_align, ref_channel_img, max_shift, blocks_per_axis, min_similarity, algorithm=algorithm, method=method)
23        return translation_mask
24
25    def save_translation_mask(self, path=None):
26        if path is None:
27            path = input("Please provide a filepath to save the translation masks") + "_translation_masks.tif"
28
29        imsave(path + "_translation_masks.tif", self.translation_masks)
30
31    def save_ccms(self, path=None):
32        if path is None:
33            path = input("Please provide a filepath to save the ccms") + "_ccms.tif"
34
35        imsave(path + "_ccms.tif", self.ccms)
36
37    def estimate(self, img_stack: array, ref_channel: int, max_shift: float,
38                 blocks_per_axis: int, min_similarity: float, method: str="subpixel",
39                 save_translation_masks: bool=True, translation_mask_save_path: str=None,
40                 save_ccms: bool=False, ccms_save_path: str=None, algorithm: str="field",
41                 apply: bool=False):
42
43        channels_to_align = list(range(img_stack.shape[0]))
44        channels_to_align.remove(ref_channel)
45
46        if ref_channel > img_stack.shape[1]:
47            print("Reference channel number cannot be bigger than number of channels!")
48            return None
49
50        self.translation_masks = np.zeros((img_stack.shape[0], img_stack.shape[1], img_stack.shape[2]*2))
51        self.ccms = []
52
53        for channel in channels_to_align:
54            translation_mask, ccm = self.calculate_translation(img_stack[channel], img_stack[ref_channel], max_shift, blocks_per_axis, min_similarity, algorithm=algorithm, method=method)
55            self.translation_masks[channel] = translation_mask
56            self.ccms.append(ccm)
57
58        self.ccms.insert(ref_channel, np.zeros((len(self.ccms[0]), len(self.ccms[0][0]))))
59        self.ccms = np.array(self.ccms)
60
61        if save_translation_masks:
62            self.save_translation_mask(path=translation_mask_save_path)
63
64        if save_ccms:
65            self.save_ccms(path=ccms_save_path)
66
67        if apply:
68            return self.apply_elastic_transform(img_stack)
69        else:
70            return None
class ChannelRegistrationEstimator:
12class ChannelRegistrationEstimator(object):
13
14    def __init__(self) -> None:
15        self.translation_masks = None
16        self.ccms = None
17
18    def apply_elastic_transform(self, img_stack):
19        corrector = ChannelRegistrationCorrector()
20        return corrector.align_channels(img_stack, translation_masks=self.translation_masks)
21
22    def calculate_translation(self, channel_to_align, ref_channel_img, max_shift, blocks_per_axis, min_similarity, algorithm="field", method="subpixel"):
23        translation_mask = calculate_translation_mask(channel_to_align, ref_channel_img, max_shift, blocks_per_axis, min_similarity, algorithm=algorithm, method=method)
24        return translation_mask
25
26    def save_translation_mask(self, path=None):
27        if path is None:
28            path = input("Please provide a filepath to save the translation masks") + "_translation_masks.tif"
29
30        imsave(path + "_translation_masks.tif", self.translation_masks)
31
32    def save_ccms(self, path=None):
33        if path is None:
34            path = input("Please provide a filepath to save the ccms") + "_ccms.tif"
35
36        imsave(path + "_ccms.tif", self.ccms)
37
38    def estimate(self, img_stack: array, ref_channel: int, max_shift: float,
39                 blocks_per_axis: int, min_similarity: float, method: str="subpixel",
40                 save_translation_masks: bool=True, translation_mask_save_path: str=None,
41                 save_ccms: bool=False, ccms_save_path: str=None, algorithm: str="field",
42                 apply: bool=False):
43
44        channels_to_align = list(range(img_stack.shape[0]))
45        channels_to_align.remove(ref_channel)
46
47        if ref_channel > img_stack.shape[1]:
48            print("Reference channel number cannot be bigger than number of channels!")
49            return None
50
51        self.translation_masks = np.zeros((img_stack.shape[0], img_stack.shape[1], img_stack.shape[2]*2))
52        self.ccms = []
53
54        for channel in channels_to_align:
55            translation_mask, ccm = self.calculate_translation(img_stack[channel], img_stack[ref_channel], max_shift, blocks_per_axis, min_similarity, algorithm=algorithm, method=method)
56            self.translation_masks[channel] = translation_mask
57            self.ccms.append(ccm)
58
59        self.ccms.insert(ref_channel, np.zeros((len(self.ccms[0]), len(self.ccms[0][0]))))
60        self.ccms = np.array(self.ccms)
61
62        if save_translation_masks:
63            self.save_translation_mask(path=translation_mask_save_path)
64
65        if save_ccms:
66            self.save_ccms(path=ccms_save_path)
67
68        if apply:
69            return self.apply_elastic_transform(img_stack)
70        else:
71            return None
translation_masks
ccms
def apply_elastic_transform(self, img_stack):
18    def apply_elastic_transform(self, img_stack):
19        corrector = ChannelRegistrationCorrector()
20        return corrector.align_channels(img_stack, translation_masks=self.translation_masks)
def calculate_translation( self, channel_to_align, ref_channel_img, max_shift, blocks_per_axis, min_similarity, algorithm='field', method='subpixel'):
22    def calculate_translation(self, channel_to_align, ref_channel_img, max_shift, blocks_per_axis, min_similarity, algorithm="field", method="subpixel"):
23        translation_mask = calculate_translation_mask(channel_to_align, ref_channel_img, max_shift, blocks_per_axis, min_similarity, algorithm=algorithm, method=method)
24        return translation_mask
def save_translation_mask(self, path=None):
26    def save_translation_mask(self, path=None):
27        if path is None:
28            path = input("Please provide a filepath to save the translation masks") + "_translation_masks.tif"
29
30        imsave(path + "_translation_masks.tif", self.translation_masks)
def save_ccms(self, path=None):
32    def save_ccms(self, path=None):
33        if path is None:
34            path = input("Please provide a filepath to save the ccms") + "_ccms.tif"
35
36        imsave(path + "_ccms.tif", self.ccms)
def estimate( self, img_stack: <built-in function array>, ref_channel: int, max_shift: float, blocks_per_axis: int, min_similarity: float, method: str = 'subpixel', save_translation_masks: bool = True, translation_mask_save_path: str = None, save_ccms: bool = False, ccms_save_path: str = None, algorithm: str = 'field', apply: bool = False):
38    def estimate(self, img_stack: array, ref_channel: int, max_shift: float,
39                 blocks_per_axis: int, min_similarity: float, method: str="subpixel",
40                 save_translation_masks: bool=True, translation_mask_save_path: str=None,
41                 save_ccms: bool=False, ccms_save_path: str=None, algorithm: str="field",
42                 apply: bool=False):
43
44        channels_to_align = list(range(img_stack.shape[0]))
45        channels_to_align.remove(ref_channel)
46
47        if ref_channel > img_stack.shape[1]:
48            print("Reference channel number cannot be bigger than number of channels!")
49            return None
50
51        self.translation_masks = np.zeros((img_stack.shape[0], img_stack.shape[1], img_stack.shape[2]*2))
52        self.ccms = []
53
54        for channel in channels_to_align:
55            translation_mask, ccm = self.calculate_translation(img_stack[channel], img_stack[ref_channel], max_shift, blocks_per_axis, min_similarity, algorithm=algorithm, method=method)
56            self.translation_masks[channel] = translation_mask
57            self.ccms.append(ccm)
58
59        self.ccms.insert(ref_channel, np.zeros((len(self.ccms[0]), len(self.ccms[0][0]))))
60        self.ccms = np.array(self.ccms)
61
62        if save_translation_masks:
63            self.save_translation_mask(path=translation_mask_save_path)
64
65        if save_ccms:
66            self.save_ccms(path=ccms_save_path)
67
68        if apply:
69            return self.apply_elastic_transform(img_stack)
70        else:
71            return None